package org.carabiner.harness;

import groovy.ui.Console;

import java.awt.event.ActionEvent;

import javax.swing.Action;

class LaunchGroovyConsoleAction extends AbstractInfoPanelAction
{  
  private Console console;

  public LaunchGroovyConsoleAction(Harness harness)
  {
    super(harness);
    putValue(Action.LONG_DESCRIPTION, "Opens a Groovy console to allow modification of the current control");
    putValue(Action.SMALL_ICON, getIcon("console.png"));
   }

  public void actionPerformed(ActionEvent e)
  {
    console = new Console();
    console.setVariable("component", getHarness().getTestSubject());
    console.setVariable("selectedComponent", getHarness().getSelectedComponent());
    console.setVariable("harness", getHarness());    
    console.run();
    StringBuffer introMessage = new StringBuffer();
    introMessage.append("Defined Variables are: \n");
    introMessage.append("component = Test Subject\n");
    introMessage.append("selectedComponent = component selected in inspector");
    console.getOutputArea().setText(introMessage.toString());    
  }
  
  Console getConsole()
  {
    return console;
  }

}
